//
//  CGDrawing.m
//  Racing
//
//  Created by vy phan on 4/15/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CGDrawing.h"


@implementation CGDrawing
#ifdef USE_CGDRAWING
CGContextRef CreateDrawingBitmapContext(int pixelsWide, int pixelsHigh)
{
	CGContextRef context = NULL;
	CGColorSpaceRef colorSpace;
	void * bitmapData;
	int bitmapByteCount;
	int bitmapBytesPerRow;
	bitmapBytesPerRow = (pixelsWide * 4);
	bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
	colorSpace = CGColorSpaceCreateDeviceRGB();
	bitmapData = malloc( bitmapByteCount );
	if (bitmapData == NULL)
	{
		NSLog( @"Memory not allocated!");
		return NULL;
	}
	context = CGBitmapContextCreate (bitmapData,
									 pixelsWide,
									 pixelsHigh,
									 8, // bits per component
									 bitmapBytesPerRow,
									 colorSpace,
									 kCGImageAlphaPremultipliedFirst);
	if (context== NULL)
	{
		free (bitmapData);
		NSLog( @"Context not created!");
		return NULL;
	}
	CGColorSpaceRelease( colorSpace );
	return context;
}

-(void)SwapTexture
{
//	if( DrawingSprite != nil && DrawingSprite.parent == self )
//	{
//		[[DrawingSprite texture] dealloc];
//		[self removeChild:DrawingSprite cleanup:YES];
//	}
	
	CGImageRef ref = CGBitmapContextCreateImage(DrawingBitmap);
	DrawingImage = [UIImage imageWithCGImage:ref];
	CGImageRelease(ref);
	DrawingTexture = [[CCTexture2D alloc] initWithImage:DrawingImage];
	DrawingSprite = [[CCSprite spriteWithTexture:DrawingTexture] retain];
	[DrawingSprite setAnchorPoint:ccp(0,0)];
	//[DrawingSprite setScale: 1/1.5];
//	[self addChild: DrawingSprite z:15];
}
-(CCSprite*) drawingRectWithX:(int)x_ Y:(int)y_ Width:(int)width_ Height:(int)height_
{
	DrawingBitmap = CreateDrawingBitmapContext(width_, height_);
	CGSize s = [[CCDirector sharedDirector] winSize];
	CGContextRef context = DrawingBitmap;
	//CGContextSetRGBFillColor(context, 1.0,1.0,1.0,0.0);
	//CGContextClearRect( context, CGRectMake( x_ - 1, y_ - 1, width_ + 1, height_ + 1));
	CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
	CGContextFillRect(context, CGRectMake( x_ - 1, y_ - 1, width_ + 1, height_ + 1));
	
	CGContextSetLineWidth(context, 2.0);
	
	CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
	
	CGRect rectangle = CGRectMake(x_,y_,width_,height_);
	
	CGContextAddRect(context, rectangle);
	
	
	
	CGContextMoveToPoint(context, x_, y_);
	CGContextAddLineToPoint(context, width_, height_);

	
	CGContextStrokePath(context);
	
	CGImageRef ref = CGBitmapContextCreateImage(DrawingBitmap);
	DrawingImage = [UIImage imageWithCGImage:ref];
	CGImageRelease(ref);
	DrawingTexture = [[CCTexture2D alloc] initWithImage:DrawingImage];
	DrawingSprite = [[CCSprite spriteWithTexture:DrawingTexture] retain];
	[DrawingSprite setAnchorPoint:ccp(0,0)];
	[DrawingSprite setScale: 1/1.5];
	
	return DrawingSprite;
}
#endif
-(void) drawingLineWithContext:(CGContextRef)context StartPoint:(CGPoint)start EndPoint:(CGPoint)end
{
	CGContextMoveToPoint(context, start.x, start.y);
	CGContextAddLineToPoint(context, end.x, end.y);
}
-(void) drawingRectWithContext:(CGContextRef)context X:(int)x_ Y:(int)y_ Width:(int)width_ Height:(int)height_
{
	CGRect rectangle = CGRectMake(x_, y_, width_, height_);
	
	CGContextAddRect(context, rectangle);
}
-(void) drawingArcWithContext:(CGContextRef)context X1:(CGPoint)point1 X2:(CGPoint)point2 X3:(CGPoint)point3 Radius:(CGFloat)radius
{
	CGContextMoveToPoint(context, point1.x, point1.y);
	CGContextAddArcToPoint(context, point2.x,point2.y, point3.x, point3.y, radius);
}
-(void) drawingPolyWithContext:(CGContextRef)context xVertices:(NSMutableArray*)xVertices yVertices:(NSMutableArray*)yVertices
{
	int yVerticesCount = [yVertices count];
	int xVerticesCount = [xVertices count];
	if (xVerticesCount != yVerticesCount) {
		NSLog(@"Number of poly x points must be equal to y points");
		return;
	}
	for (int i = 0; i < xVerticesCount; i++) {
		int xPoint = [[xVertices objectAtIndex:i] intValue];
		int yPoint = [[yVertices objectAtIndex:i] intValue];
		if (i == 0) {
			CGContextMoveToPoint(context, xPoint, yPoint);
		}
		
		// Draw closed poly
		xPoint = [[xVertices objectAtIndex:(i+1)%xVerticesCount] intValue];
		yPoint = [[yVertices objectAtIndex:(i+1)%xVerticesCount] intValue];
		CGContextAddLineToPoint(context, xPoint, yPoint);
	}
}
@end
